export function getActorParam(_tempData:any,_actorData:any,_thingmodel:any,host:any,isInit:boolean) {
	//var start = new Date().getTime()
	let param={};
	for(var item of _thingmodel ){
		getField(_tempData,_actorData,null,param,item,host,isInit)
	} 
	//var end = new Date().getTime()
	//console.log('cost is', `${end - start}ms`)
	return param;
}

export function getField(_tempData:any,_actorData:any,pField:any,field:any,data:any,host:any,isInit:boolean){
	if(pField){
		if(pField.hasOwnProperty("pidentifier")){
			data['pidentifier']=pField["pidentifier"]+"."+pField.identifier
		}else{
			data['pidentifier']=pField.identifier
		}
	}
	if(data.dataType.type=='struct'){
		let param={};
		if(data.dataType.specs){
			for(var item of data.dataType.specs ){
				getField(_tempData!=null && _tempData.hasOwnProperty(data.identifier)?_tempData[data.identifier]:{},_actorData!=null && _actorData.hasOwnProperty(data.identifier)?_actorData[data.identifier]:{},data,param,item,host,isInit)
			}
		}
		field[data.identifier]=param
	}else{
		let value=getValue(data,_tempData,_actorData,host,isInit);
		if(null!=value && undefined!=value && !(value==='')){
			if(data.dataType.type=='enum'){
				data.dataType['value']=value+"";
			}else{
				data.dataType['value']=value;
			}
			field[data.identifier]=value;
		}else{
			if(isInit==true){
				field[data.identifier]=value;
			}
		}
	}
}

export function getValue(field:any,_tempData:any,_actorData:any,host:any,isInit:boolean){
	if(_actorData!=null && _actorData.hasOwnProperty(field.identifier)){
		if(!field.readonly){
			return convert(field.dataType.type,_actorData[field.identifier],host);
		}else{
			if(_tempData!=null && _tempData.hasOwnProperty(field.identifier)){
				return convert(field.dataType.type,_tempData[field.identifier],host);
			}else{
				return convert(field.dataType.type,field.defaultValue,host);
			}
		}
 	}else{
		if(isInit || field.readonly){
			if(_tempData!=null && _tempData.hasOwnProperty(field.identifier)){
				return convert(field.dataType.type,_tempData[field.identifier],host);
			}else{
				return convert(field.dataType.type,field.defaultValue,host);
			}
		}
	}
}
	
export function convert(type:any,value:any,host:any){
	if(type=='int' ){
		return isNaN(parseInt(value))?0:parseInt(value);
	}
	else if(type=='double' || type=='float' ){
		return isNaN(parseFloat(value))?0:parseFloat(value);
	}
	else if(type=='string' || type=='v4' || type=='v3' || type=='v2' || type=='date' || type=='color' || type=='layer' || type=='assembly' || type=='actor'){
		return value
	}
	else if(type=='struct'){
	}
	else if(type=='boolean'){
		if('0'==value){
			return false;
		}
		else if('1'==value){
			return true;
		}
		else if('false'==value){
			return Boolean(value);
		}
		else if('true'==value){
			return Boolean(value);
		}
		else{
			return value
		}
	}
	else if(type=='enum'){
		let v=parseInt(value);
		if(isNaN(v)){
			return value
		}else{
			return v;
		}
	}
	else if(type=='array'){
		return value
	}
	else if(type=='img'){
		if(host){
			if(value.indexOf("http") == -1 && value.indexOf("https") == -1 && value.indexOf("HTTP") == -1 && value.indexOf("HTTPS") == -1){
				return host+'/file/thumbnail/'+value;
			}
			return value
		}else{
			return value
		}
	}
	else if(type=='model'){
		if(host){
			if(value.indexOf("http") == -1 && value.indexOf("https") == -1 && value.indexOf("HTTP") == -1 && value.indexOf("HTTPS") == -1){
				return host+'/file/model/'+value;
			}
			return value
		}else{
			return value
		}
	}else{
		return value
	}
}

// 转换方法
export function transformData(configs:any, data:any) {
	console.log("data=",data);
   let result:any = {};
    configs.forEach((config:any) => {
        const { source, target, pSource, pTarget } = config;

        // (1) 获取源数据的值（支持多级 pSource）
        let sourceValue;
        if (pSource) {
            const sourcePath = pSource.split(',');
            sourceValue = sourcePath.reduce((obj:any, key:any) => obj?.[key], data);
            sourceValue = sourceValue?.[source]; // 最后取 source 字段
        } else {
            sourceValue = data[source];
        }

        if (sourceValue === undefined) return; // 如果源数据不存在，跳过

        // (2) 构造目标路径（支持多级 pTarget）
        const targetPath = pTarget ? pTarget.split(',') : [];
        let currentTarget = result;

        // 遍历并创建嵌套结构（除了最后一层）
        for (let i = 0; i < targetPath.length; i++) {
            const key = targetPath[i];
            if (!currentTarget[key]) {
                currentTarget[key] = {};
            }
            currentTarget = currentTarget[key];
        }

        // (3) 设置目标值
        currentTarget[target] = sourceValue;
    });

    return result;
}